package com.university.evaluation.controller;

import com.university.evaluation.entity.EvaluationRule;
import com.university.evaluation.service.EvaluationRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考评规则控制器
 * 实现需求文档中的规则配置API
 */
@Slf4j
@RestController
@RequestMapping("/api/rules")
@RequiredArgsConstructor
@Tag(name = "考评规则管理", description = "考评规则配置相关接口")
public class RuleController {

    private final EvaluationRuleService ruleService;

    /**
     * 添加学院考评规则
     * POST /api/rules/college
     */
    @PostMapping("/college")
    @Operation(summary = "添加学院考评规则", description = "管理员为学院配置考评规则")
    public ResponseEntity<ApiResponse<String>> addCollegeRule(@RequestBody AddRuleRequest request) {
        try {
            log.info("添加学院考评规则：collegeId={}, semester={}", request.getCollegeId(), request.getSemester());
            
            String ruleId = ruleService.addCollegeRule(request.getCollegeId(), request.getIndicators(), request.getSemester());
            
            return ResponseEntity.ok(new ApiResponse<>(true, "规则添加成功", ruleId));
            
        } catch (EvaluationRuleService.BusinessException e) {
            log.error("添加学院考评规则失败：{}", e.getMessage());
            return ResponseEntity.badRequest().body(new ApiResponse<>(false, e.getMessage(), null));
        } catch (Exception e) {
            log.error("添加学院考评规则失败：", e);
            return ResponseEntity.badRequest().body(new ApiResponse<>(false, "规则添加失败：" + e.getMessage(), null));
        }
    }

    /**
     * 调整考评维度权重
     * PUT /api/rules/{ruleId}/adjust
     */
    @PutMapping("/{ruleId}/adjust")
    @Operation(summary = "调整考评维度权重", description = "管理员调整现有规则的指标权重")
    public ResponseEntity<ApiResponse<String>> adjustRule(
            @PathVariable String ruleId,
            @RequestBody List<EvaluationRuleService.Indicator> indicators) {
        
        try {
            log.info("调整考评规则权重：ruleId={}", ruleId);
            
            EvaluationRuleService.RuleAdjustResult result = ruleService.adjustRule(ruleId, indicators);
            
            if (result.isSuccess()) {
                return ResponseEntity.ok(new ApiResponse<>(true, result.getMessage(), ruleId));
            } else {
                return ResponseEntity.badRequest().body(new ApiResponse<>(false, result.getMessage(), null));
            }
            
        } catch (Exception e) {
            log.error("调整考评规则权重失败：ruleId={}, error={}", ruleId, e.getMessage(), e);
            return ResponseEntity.badRequest().body(new ApiResponse<>(false, "权重调整失败：" + e.getMessage(), null));
        }
    }

    /**
     * 获取学院当前考评规则
     * GET /api/rules/college/{collegeId}/current
     */
    @GetMapping("/college/{collegeId}/current")
    @Operation(summary = "获取学院当前考评规则", description = "查询学院当前生效的考评规则")
    public ResponseEntity<ApiResponse<EvaluationRule>> getCurrentRule(
            @PathVariable String collegeId,
            @RequestParam String semester) {
        
        try {
            EvaluationRule rule = ruleService.getCurrentRule(collegeId, semester);
            
            if (rule != null) {
                return ResponseEntity.ok(new ApiResponse<>(true, "查询成功", rule));
            } else {
                return ResponseEntity.ok(new ApiResponse<>(false, "未找到该学院在此学期的考评规则", null));
            }
            
        } catch (Exception e) {
            log.error("查询学院考评规则失败：collegeId={}, semester={}, error={}", collegeId, semester, e.getMessage(), e);
            return ResponseEntity.badRequest().body(new ApiResponse<>(false, "查询失败：" + e.getMessage(), null));
        }
    }

    /**
     * 添加规则请求类
     */
    public static class AddRuleRequest {
        private String collegeId;
        private List<EvaluationRuleService.Indicator> indicators;
        private String semester;

        // Getters and Setters
        public String getCollegeId() { return collegeId; }
        public void setCollegeId(String collegeId) { this.collegeId = collegeId; }
        public List<EvaluationRuleService.Indicator> getIndicators() { return indicators; }
        public void setIndicators(List<EvaluationRuleService.Indicator> indicators) { this.indicators = indicators; }
        public String getSemester() { return semester; }
        public void setSemester(String semester) { this.semester = semester; }
    }

    /**
     * 通用API响应类
     */
    public static class ApiResponse<T> {
        private boolean success;
        private String message;
        private T data;

        public ApiResponse(boolean success, String message, T data) {
            this.success = success;
            this.message = message;
            this.data = data;
        }

        // Getters
        public boolean isSuccess() { return success; }
        public String getMessage() { return message; }
        public T getData() { return data; }
    }
}
